﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using TimerTask.Common.Utils;
using TimerTask.Model.Entity;

namespace TimerTask.Dal
{
    public class TriggerLogDal : BaseDal
    {
        /// <summary>
        /// 增加触发记录
        /// author:肖阳
        /// date:2016-10-31 11:40:58
        /// </summary>
        /// <param name="model"></param>
        public void AddTriggerLog(TriggerLog model)
        {
            string sql =
                @"INSERT INTO TriggerLog (JobId, TriggerTime, TriggerResult, TriggerStatus) 
                VALUES (?JobId, ?TriggerTime, ?TriggerResult, ?TriggerStatus);";
            try
            {
                DbConnection.Execute(sql, model);
            }
            catch (Exception exception)
            {
                LogUtil.Error(exception, "增加触发记录失败{0}", model.ToJson());
            }
        }


        public List<Tuple<string, string, DateTime>> GetFailLog()
        {
            string sql = @"SELECT ji.JobName, tl.TriggerResult,tl.TriggerTime
FROM triggerlog tl LEFT JOIN jobinfo ji on ji.Id= tl.JobId  WHERE TriggerStatus=2 ORDER BY TriggerTime DESC LIMIT  10;";
            return
                DbConnection.Query<string, string, DateTime, Tuple<string, string, DateTime>>(sql, Tuple.Create,
                    splitOn: "*").ToList();
        }
    }
}